{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# <center>第十章 多元分析</center>\n",
    "## <center>第一节 聚类分析</center>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**介绍**  \n",
    "这里是司守奎教授的《数学建模算法与应用》全书案例代码python实现，欢迎加入此项目将其案例代码用python实现  \n",
    "GitHub项目地址：[Mathematical-modeling-algorithm-and-Application](https://github.com/STL-CC/Mathematical-modeling-algorithm-and-Application)  \n",
    "CSDN专栏：[数学建模](https://blog.csdn.net/stl_cc/category_10228778.html)  \n",
    "知乎专栏：[数学建模算法与应用](https://zhuanlan.zhihu.com/c_1271013077337964544)  \n",
    "**联系作者**  \n",
    "作者：STL_CC  \n",
    "邮箱：<1459078309@qq.com>  \n",
    "\n",
    "由于作者还是大一学生，才疏学浅，难免会有错误，欢迎指正  \n",
    "同时作者精力有限，希望更多大佬加入此项目，一来可以提高建模水平，二来可以分享建模经验"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### **系统聚类法**  \n",
    "**例10.1**  \n",
    "设有5个销售员$w_1$,$w_2$,$w_3$,$w_4$,$w_5$,他们的销售业绩由二维变量($v_1$,$v_2$)描述,见表1 销售员业绩表 "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "|  销售员   | $v_1$(销售量)百件  |$v_2$(回收款项)万元|\n",
    "|  :----:  | :----:  |:----:|\n",
    "|  $w_1$ | 1 |0|\n",
    "| $w_2$  | 1 |1|\n",
    "| $w_3$  | 3 |2|\n",
    "| $w_4$  | 4 |3|\n",
    "| $w_5$  |2 |5|"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**方法一**  系统聚类法中的最长距离法"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import numpy.matlib\n",
    "import pandas as pd\n",
    "import matplotlib\n",
    "import matplotlib.pyplot as plt\n",
    "from scipy.spatial.distance import pdist,squareform\n",
    "from scipy.cluster.hierarchy import dendrogram, linkage,fcluster"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "a = np.array([[1,0],[1,1],[3,2],[4,3],[2,5]])\n",
    "(m,n)=a.shape\n",
    "d = np.zeros((m,m))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "for i in range(m):\n",
    "    for j in range(i+1,m):\n",
    "        d[i,j]=sum(map(abs,(a[i,:]-a[j,:])))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "nd=d.ravel()[np.flatnonzero(d)]\n",
    "nd=np.unique(nd)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "第1次合成,平台高度为1.0时的分类结果为:[0 1]\n",
      "第2次合成,平台高度为2.0时的分类结果为:[2 3]\n",
      "第3次合成,平台高度为3.0时的分类结果为:[1 2]\n",
      "第4次合成,平台高度为4.0时的分类结果为:[0 2 3 4]\n",
      "第5次合成,平台高度为5.0时的分类结果为:[1 3 4]\n",
      "第6次合成,平台高度为6.0时的分类结果为:[0 3 4]\n"
     ]
    }
   ],
   "source": [
    "l=len(nd)\n",
    "for i in range(0,l):\n",
    "    nd_min=min(nd)\n",
    "    (row,col)=np.where(d==nd_min)\n",
    "    tm=np.union1d(row,col)\n",
    "    print('第{:d}次合成,平台高度为{}时的分类结果为:{}'.format(i+1,nd_min,tm))\n",
    "    nd=np.delete(nd,np.where(nd==min(nd)))\n",
    "    if(not len(nd)):\n",
    "        break"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**方法二** 使用python的scipy库"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import pandas as pd\n",
    "import matplotlib\n",
    "import matplotlib.pyplot as plt\n",
    "from scipy.spatial.distance import pdist,squareform\n",
    "from scipy.cluster.hierarchy import dendrogram, linkage,fcluster"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "a = np.array([[1,0],[1,1],[3,2],[4,3],[2,5]])\n",
    "y=pdist(a,'cityblock')\n",
    "yc=squareform(y)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'icoord': [[15.0, 15.0, 25.0, 25.0],\n",
       "  [35.0, 35.0, 45.0, 45.0],\n",
       "  [20.0, 20.0, 40.0, 40.0],\n",
       "  [5.0, 5.0, 30.0, 30.0]],\n",
       " 'dcoord': [[0.0, 1.0, 1.0, 0.0],\n",
       "  [0.0, 2.0, 2.0, 0.0],\n",
       "  [1.0, 3.0, 3.0, 2.0],\n",
       "  [0.0, 4.0, 4.0, 3.0]],\n",
       " 'ivl': ['4', '0', '1', '2', '3'],\n",
       " 'leaves': [4, 0, 1, 2, 3],\n",
       " 'color_list': ['g', 'r', 'b', 'b']}"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAXQAAAD7CAYAAAB68m/qAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADh0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uMy4xLjEsIGh0dHA6Ly9tYXRwbG90bGliLm9yZy8QZhcZAAAQk0lEQVR4nO3dfYydZZ3G8e+1bRUjKlnpCukLNSubXd8YZYIastmJ60ZkXfhDTErXFwxmNihRsiYbMC4qu5uNyUaNwkrGlIBoFYPoVsQYXGyUP0SnOIC16DauSgOEEbBQgbrF3/5xDjpOz3TOtKd9yt3vJ3nS5+U+51yZttfc88xzzpOqQ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X/9Ap4k6kuQk4B/oTXruT7K7v/x9x9G6UPROr+wEHgb+A7ioqv6r01QD+OFcktSIo26GLkmtstAlqREWuiQ1wkKXpEZY6JLUCAtdkhphoUtSIyx0SWrE/wNVvUBvWh4H8AAAAABJRU5ErkJggg==\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "z=linkage(y)\n",
    "dendrogram(z)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [],
   "source": [
    "T = fcluster(z,3,'maxclust')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "第1类的有 (array([0, 1], dtype=int64),)\n",
      "第2类的有 (array([2, 3], dtype=int64),)\n",
      "第3类的有 (array([4], dtype=int64),)\n"
     ]
    }
   ],
   "source": [
    "for i in range(1,3+1):\n",
    "    tm=np.where(T==i)\n",
    "    print('第{}类的有'.format(i),tm)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### **变量聚类法**  \n",
    "**例10.2**  服装标准制定中的变量聚类法。  \n",
    "在服装标准制定中，对某地成年女子的各部位尺寸进行了统计，通过14个部位的测量资料，获得各因素之间的相关系数表（见表5）。  \n",
    "注：具体资料见教材，部分数据将在程序中给出"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import pandas as pd\n",
    "import matplotlib\n",
    "import matplotlib.pyplot as plt\n",
    "from scipy.spatial.distance import pdist,squareform\n",
    "from scipy.cluster.hierarchy import dendrogram, linkage,fcluster"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [],
   "source": [
    "a=pd.read_csv('ch.txt',header=None,sep='\t')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>0</th>\n",
       "      <th>1</th>\n",
       "      <th>2</th>\n",
       "      <th>3</th>\n",
       "      <th>4</th>\n",
       "      <th>5</th>\n",
       "      <th>6</th>\n",
       "      <th>7</th>\n",
       "      <th>8</th>\n",
       "      <th>9</th>\n",
       "      <th>10</th>\n",
       "      <th>11</th>\n",
       "      <th>12</th>\n",
       "      <th>13</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <td>0</td>\n",
       "      <td>1.000</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>1</td>\n",
       "      <td>0.366</td>\n",
       "      <td>1.000</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>2</td>\n",
       "      <td>0.242</td>\n",
       "      <td>0.233</td>\n",
       "      <td>1.000</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>3</td>\n",
       "      <td>0.280</td>\n",
       "      <td>0.194</td>\n",
       "      <td>0.590</td>\n",
       "      <td>1.000</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>4</td>\n",
       "      <td>0.360</td>\n",
       "      <td>0.324</td>\n",
       "      <td>0.476</td>\n",
       "      <td>0.435</td>\n",
       "      <td>1.000</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>5</td>\n",
       "      <td>0.282</td>\n",
       "      <td>0.262</td>\n",
       "      <td>0.483</td>\n",
       "      <td>0.470</td>\n",
       "      <td>0.452</td>\n",
       "      <td>1.000</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>6</td>\n",
       "      <td>0.245</td>\n",
       "      <td>0.265</td>\n",
       "      <td>0.540</td>\n",
       "      <td>0.478</td>\n",
       "      <td>0.535</td>\n",
       "      <td>0.663</td>\n",
       "      <td>1.000</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>7</td>\n",
       "      <td>0.448</td>\n",
       "      <td>0.345</td>\n",
       "      <td>0.452</td>\n",
       "      <td>0.404</td>\n",
       "      <td>0.431</td>\n",
       "      <td>0.322</td>\n",
       "      <td>0.266</td>\n",
       "      <td>1.000</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>8</td>\n",
       "      <td>0.486</td>\n",
       "      <td>0.367</td>\n",
       "      <td>0.365</td>\n",
       "      <td>0.357</td>\n",
       "      <td>0.429</td>\n",
       "      <td>0.283</td>\n",
       "      <td>0.287</td>\n",
       "      <td>0.820</td>\n",
       "      <td>1.000</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>9</td>\n",
       "      <td>0.648</td>\n",
       "      <td>0.662</td>\n",
       "      <td>0.216</td>\n",
       "      <td>0.032</td>\n",
       "      <td>0.429</td>\n",
       "      <td>0.283</td>\n",
       "      <td>0.263</td>\n",
       "      <td>0.527</td>\n",
       "      <td>0.547</td>\n",
       "      <td>1.000</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>10</td>\n",
       "      <td>0.689</td>\n",
       "      <td>0.671</td>\n",
       "      <td>0.243</td>\n",
       "      <td>0.313</td>\n",
       "      <td>0.430</td>\n",
       "      <td>0.302</td>\n",
       "      <td>0.294</td>\n",
       "      <td>0.520</td>\n",
       "      <td>0.558</td>\n",
       "      <td>0.957</td>\n",
       "      <td>1.000</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>11</td>\n",
       "      <td>0.486</td>\n",
       "      <td>0.636</td>\n",
       "      <td>0.174</td>\n",
       "      <td>0.243</td>\n",
       "      <td>0.375</td>\n",
       "      <td>0.296</td>\n",
       "      <td>0.255</td>\n",
       "      <td>0.403</td>\n",
       "      <td>0.417</td>\n",
       "      <td>0.857</td>\n",
       "      <td>0.852</td>\n",
       "      <td>1.000</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>12</td>\n",
       "      <td>0.133</td>\n",
       "      <td>0.153</td>\n",
       "      <td>0.732</td>\n",
       "      <td>0.477</td>\n",
       "      <td>0.339</td>\n",
       "      <td>0.392</td>\n",
       "      <td>0.446</td>\n",
       "      <td>0.266</td>\n",
       "      <td>0.241</td>\n",
       "      <td>0.054</td>\n",
       "      <td>0.099</td>\n",
       "      <td>0.055</td>\n",
       "      <td>1.000</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>13</td>\n",
       "      <td>0.376</td>\n",
       "      <td>0.252</td>\n",
       "      <td>0.676</td>\n",
       "      <td>0.581</td>\n",
       "      <td>0.441</td>\n",
       "      <td>0.447</td>\n",
       "      <td>0.440</td>\n",
       "      <td>0.424</td>\n",
       "      <td>0.372</td>\n",
       "      <td>0.363</td>\n",
       "      <td>0.376</td>\n",
       "      <td>0.321</td>\n",
       "      <td>0.627</td>\n",
       "      <td>1.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "       0      1      2      3      4      5      6      7      8      9   \\\n",
       "0   1.000    NaN    NaN    NaN    NaN    NaN    NaN    NaN    NaN    NaN   \n",
       "1   0.366  1.000    NaN    NaN    NaN    NaN    NaN    NaN    NaN    NaN   \n",
       "2   0.242  0.233  1.000    NaN    NaN    NaN    NaN    NaN    NaN    NaN   \n",
       "3   0.280  0.194  0.590  1.000    NaN    NaN    NaN    NaN    NaN    NaN   \n",
       "4   0.360  0.324  0.476  0.435  1.000    NaN    NaN    NaN    NaN    NaN   \n",
       "5   0.282  0.262  0.483  0.470  0.452  1.000    NaN    NaN    NaN    NaN   \n",
       "6   0.245  0.265  0.540  0.478  0.535  0.663  1.000    NaN    NaN    NaN   \n",
       "7   0.448  0.345  0.452  0.404  0.431  0.322  0.266  1.000    NaN    NaN   \n",
       "8   0.486  0.367  0.365  0.357  0.429  0.283  0.287  0.820  1.000    NaN   \n",
       "9   0.648  0.662  0.216  0.032  0.429  0.283  0.263  0.527  0.547  1.000   \n",
       "10  0.689  0.671  0.243  0.313  0.430  0.302  0.294  0.520  0.558  0.957   \n",
       "11  0.486  0.636  0.174  0.243  0.375  0.296  0.255  0.403  0.417  0.857   \n",
       "12  0.133  0.153  0.732  0.477  0.339  0.392  0.446  0.266  0.241  0.054   \n",
       "13  0.376  0.252  0.676  0.581  0.441  0.447  0.440  0.424  0.372  0.363   \n",
       "\n",
       "       10     11     12   13  \n",
       "0     NaN    NaN    NaN  NaN  \n",
       "1     NaN    NaN    NaN  NaN  \n",
       "2     NaN    NaN    NaN  NaN  \n",
       "3     NaN    NaN    NaN  NaN  \n",
       "4     NaN    NaN    NaN  NaN  \n",
       "5     NaN    NaN    NaN  NaN  \n",
       "6     NaN    NaN    NaN  NaN  \n",
       "7     NaN    NaN    NaN  NaN  \n",
       "8     NaN    NaN    NaN  NaN  \n",
       "9     NaN    NaN    NaN  NaN  \n",
       "10  1.000    NaN    NaN  NaN  \n",
       "11  0.852  1.000    NaN  NaN  \n",
       "12  0.099  0.055  1.000  NaN  \n",
       "13  0.376  0.321  0.627  1.0  "
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [],
   "source": [
    "d=1-abs(a)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [],
   "source": [
    "for i in range(len(d)):\n",
    "    for j in range(i+1,len(d)):\n",
    "        d.iloc[i,j]=d.iloc[j,i]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "D:\\Program Files\\anaconda\\lib\\site-packages\\ipykernel_launcher.py:1: ClusterWarning: scipy.cluster: The symmetric non-negative hollow observation matrix looks suspiciously like an uncondensed distance matrix\n",
      "  \"\"\"Entry point for launching an IPython kernel.\n"
     ]
    }
   ],
   "source": [
    "z=linkage(d,'complete')\n",
    "y=fcluster(z,2,'maxclust')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'icoord': [[25.0, 25.0, 35.0, 35.0],\n",
       "  [15.0, 15.0, 30.0, 30.0],\n",
       "  [5.0, 5.0, 22.5, 22.5],\n",
       "  [55.0, 55.0, 65.0, 65.0],\n",
       "  [45.0, 45.0, 60.0, 60.0],\n",
       "  [13.75, 13.75, 52.5, 52.5],\n",
       "  [85.0, 85.0, 95.0, 95.0],\n",
       "  [75.0, 75.0, 90.0, 90.0],\n",
       "  [125.0, 125.0, 135.0, 135.0],\n",
       "  [115.0, 115.0, 130.0, 130.0],\n",
       "  [105.0, 105.0, 122.5, 122.5],\n",
       "  [82.5, 82.5, 113.75, 113.75],\n",
       "  [33.125, 33.125, 98.125, 98.125]],\n",
       " 'dcoord': [[0.0, 0.29809729955167313, 0.29809729955167313, 0.0],\n",
       "  [0.0, 0.4028870809544531, 0.4028870809544531, 0.29809729955167313],\n",
       "  [0.0, 0.7535436284648688, 0.7535436284648688, 0.4028870809544531],\n",
       "  [0.0, 0.28955310393777517, 0.28955310393777517, 0.0],\n",
       "  [0.0, 0.9263082640244553, 0.9263082640244553, 0.28955310393777517],\n",
       "  [0.7535436284648688,\n",
       "   1.158768311613672,\n",
       "   1.158768311613672,\n",
       "   0.9263082640244553],\n",
       "  [0.0, 0.49697082409332655, 0.49697082409332655, 0.0],\n",
       "  [0.0, 0.8415325305655154, 0.8415325305655154, 0.49697082409332655],\n",
       "  [0.0, 0.5600321419347287, 0.5600321419347287, 0.0],\n",
       "  [0.0, 0.7182241989796779, 0.7182241989796779, 0.5600321419347287],\n",
       "  [0.0, 0.850338756026091, 0.850338756026091, 0.7182241989796779],\n",
       "  [0.8415325305655154,\n",
       "   1.2178604189314965,\n",
       "   1.2178604189314965,\n",
       "   0.850338756026091],\n",
       "  [1.158768311613672,\n",
       "   2.1077737070188536,\n",
       "   2.1077737070188536,\n",
       "   1.2178604189314965]],\n",
       " 'ivl': ['1',\n",
       "  '11',\n",
       "  '9',\n",
       "  '10',\n",
       "  '0',\n",
       "  '7',\n",
       "  '8',\n",
       "  '4',\n",
       "  '5',\n",
       "  '6',\n",
       "  '12',\n",
       "  '3',\n",
       "  '2',\n",
       "  '13'],\n",
       " 'leaves': [1, 11, 9, 10, 0, 7, 8, 4, 5, 6, 12, 3, 2, 13],\n",
       " 'color_list': ['g',\n",
       "  'g',\n",
       "  'g',\n",
       "  'g',\n",
       "  'g',\n",
       "  'g',\n",
       "  'r',\n",
       "  'r',\n",
       "  'r',\n",
       "  'r',\n",
       "  'r',\n",
       "  'r',\n",
       "  'b']}"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/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qe7voO+RiLg1Ik6JiKdGxGlkZ2fdmAdoE3D8xELev/5MOvuYeRewn6SFhbLjO6yzkdfTnbP5Q8km27s4D8pfkn0JWFWQ9ZN9wfgLSduAdwBnSvp+RfU30my22KlXFLGD7KSj65fN5yPAvglcFBGf7uKh9iN731ci7zO/guzTz5n5yVu3PYHsU3/Dqd5b7bhXkrSfpAPI5t+ZIemATobHTVZfPpzqgHzT/fN1Hf1SSTour+dJkt5BNvpjdRfafy3wfEln5usvAG7t5GNmfnZxDbBS0oGSTia7yUxlv6iS/pjsDV31aBvyM9SfAm/KX7dDyL4L+OHke5Y2SBYqtfxxGfBfQCW30pR0iKTTJv6PJZ1F1r99fRX15z4JvEXS0yTNAt5GNkqsnfY2fG/m343cAFwSEZdV1fC8zUslHSRphrJbmC7Lj1WVS4GjgVdGxKOtNp7MJK/PqZJOyH+Gg8k+Ve0Abp/yQSJir3yQfesddY/3dKM+sm/S69ct6LD9H8j/0x4m+9j6rG69HmTdCHeQfSQf7rTteZ2HkvVp/wr4BbC84v/fy4FPd/H9U8tfix3AfWR/UJ7WxffqZyqsbw7ZkM2HyEZl3AKcWnGbZ5INQb2frHvxo8ABHfz8u703gQvz5w8XHxW9Pt/O2/4gsBH4mwpfm6fn7f51XdvPqvj1+cv89/ZhYJzsy+Tj2jmGJzUzM0vcXtt1Y2Zm5TjozcwS56A3M0ucg97MLHEOejOzxDnozcwS56A3M0ucg97MLHH/DzrFfGuov9URAAAAAElFTkSuQmCC\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "dendrogram(z)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**说明**  \n",
    "1.在教材原来的matlab程序的数据预处理中，原始数据被去掉了0，向量长度不一。然而这样长度不一的向量是不能在python中被处理的。笔者将其转换为了一个对称矩阵再行操作，更能适应python操作。  \n",
    "2.在matlab中，相关系数矩阵计算使用的corrcoef列指标是变量，然而np.corrcoef行指标是向量，所以要先转置  \n",
    "**参考文献**  \n",
    "1.[python中scipy库实现计算各种距离](https://blog.csdn.net/Jiajikang_jjk/article/details/83474446)   \n",
    "2.[Python之向量（Vector）距离矩阵计算](https://zhuanlan.zhihu.com/p/77034615)  \n",
    "3.[scipy.spatial.distance.pdist官方文档](https://docs.scipy.org/doc/scipy/reference/generated/scipy.spatial.distance.pdist.html)  \n",
    "4.[使用Python进行层次聚类（一）——基本使用+主成分分析绘图观察结果+绘制热图](https://blog.csdn.net/qq_19528953/article/details/79133889)  \n",
    "5.[python的scipy层次聚类参数详解](https://blog.csdn.net/enigma_tong/article/details/79081449)  \n",
    "6.[皮尔逊积矩相关系数--NumPy--corrcoef()](https://blog.csdn.net/weixin_38287297/article/details/81501529)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### **案例研究**  \n",
    "**聚类分析案例—我国各地区普通高等教育发展状况分析**  \n",
    "具体资料见教材，部分数据将在程序中给出"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**R型聚类分析**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import pandas as pd\n",
    "import matplotlib\n",
    "import matplotlib.pyplot as plt\n",
    "from scipy.spatial.distance import pdist,squareform\n",
    "from scipy.cluster.hierarchy import dendrogram, linkage,fcluster"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [],
   "source": [
    "gj=pd.read_csv('gj.txt',header=None,sep='\t').T"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "D:\\Program Files\\anaconda\\lib\\site-packages\\ipykernel_launcher.py:4: ClusterWarning: scipy.cluster: The symmetric non-negative hollow observation matrix looks suspiciously like an uncondensed distance matrix\n",
      "  after removing the cwd from sys.path.\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "{'icoord': [[5.0, 5.0, 15.0, 15.0],\n",
       "  [55.0, 55.0, 65.0, 65.0],\n",
       "  [85.0, 85.0, 95.0, 95.0],\n",
       "  [75.0, 75.0, 90.0, 90.0],\n",
       "  [60.0, 60.0, 82.5, 82.5],\n",
       "  [45.0, 45.0, 71.25, 71.25],\n",
       "  [35.0, 35.0, 58.125, 58.125],\n",
       "  [25.0, 25.0, 46.5625, 46.5625],\n",
       "  [10.0, 10.0, 35.78125, 35.78125]],\n",
       " 'dcoord': [[0.0, 0.8525484864006131, 0.8525484864006131, 0.0],\n",
       "  [0.0, 0.024863305626884467, 0.024863305626884467, 0.0],\n",
       "  [0.0, 0.02757546216575882, 0.02757546216575882, 0.0],\n",
       "  [0.0, 0.04009972160627508, 0.04009972160627508, 0.02757546216575882],\n",
       "  [0.024863305626884467,\n",
       "   0.14222971620143948,\n",
       "   0.14222971620143948,\n",
       "   0.04009972160627508],\n",
       "  [0.0, 0.3129510501346711, 0.3129510501346711, 0.14222971620143948],\n",
       "  [0.0, 0.4732207840475411, 0.4732207840475411, 0.3129510501346711],\n",
       "  [0.0, 1.0379457746449512, 1.0379457746449512, 0.4732207840475411],\n",
       "  [0.8525484864006131,\n",
       "   1.7688040008982475,\n",
       "   1.7688040008982475,\n",
       "   1.0379457746449512]],\n",
       " 'ivl': ['6', '7', '9', '8', '0', '4', '5', '1', '2', '3'],\n",
       " 'leaves': [6, 7, 9, 8, 0, 4, 5, 1, 2, 3],\n",
       " 'color_list': ['g', 'r', 'r', 'r', 'r', 'r', 'r', 'r', 'b']}"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAXoAAAD7CAYAAABkO19ZAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADh0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2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aue41+jN53OJvHWUs3zPoFetcEe8cwn7uZoK+qh+kF2XI51ffNjKc3D0BV3VlV51fVi6rqInq9uFFfZ+hkehe1+2AXLA8Bv8Nog+U+YHWSs/razmH0H9HXAWuBbyR5APhl4I1J/nzEdcy10FVmm9fdM+Oj9EYD3th1kJaD1ThGf1i/A/xikhcnOQn4JXpHnoxUkn9K76PxSI+2mVPDq5Oc0I2N/zK9o16uG2UN3aeqrwHvSLI6yQvpfW/x5RHW8Di9TxRbkpyY5Dx6N8T52Khq6EzT+8872U3XAv8LGNntNZO8MMlF3etidZLL6R2ZdvOoaujqWJ3kBHrXzlp1qJ5R1tD5MHA28DNV9cRiKx8LXVZtSPK8JKvSu93qZcCtQ91RVTUz0Ruj/xC9oxoeAH4bOGEMdXwE+NiY/y3eCzxMb+jiM8CPjKmOSWBnV8u36L35vXjENZxM73uBx4FvABvH+bfpano38PER73OC3mGdj3b/R24HLhjT715zpnePuIYzuv1+t/s/cmi6fAx/k893f49vA3cB/3rY+/GiZpLUuNaGbiRJcxj0ktQ4g16SGmfQS1LjDHpJapxBL0mNM+glqXEGvSQ17v8DI/SSDjQmds8AAAAASUVORK5CYII=\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "r=np.corrcoef(gj)\n",
    "d=1-r\n",
    "d=d.T\n",
    "z=linkage(d,'average')\n",
    "dendrogram(z)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [],
   "source": [
    "T=fcluster(z,6,'maxclust')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "第1类的有 (array([6], dtype=int64),)\n",
      "第2类的有 (array([7], dtype=int64),)\n",
      "第3类的有 (array([1, 2, 3, 4, 5], dtype=int64),)\n",
      "第4类的有 (array([0], dtype=int64),)\n",
      "第5类的有 (array([8], dtype=int64),)\n",
      "第6类的有 (array([9], dtype=int64),)\n"
     ]
    }
   ],
   "source": [
    "for i in range(1,6+1):\n",
    "    tm=np.where(T==i)\n",
    "    print('第{}类的有'.format(i),tm)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**Q型聚类分析**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import pandas as pd\n",
    "import matplotlib\n",
    "import matplotlib.pyplot as plt\n",
    "from scipy.spatial.distance import pdist,squareform\n",
    "from scipy.cluster.hierarchy import dendrogram, linkage,fcluster\n",
    "from scipy.stats import zscore"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [],
   "source": [
    "gj=pd.read_csv('gj.txt',header=None,sep='\t')\n",
    "gj.drop([2,3,4,5],axis=1,inplace=True)\n",
    "gj=zscore(gj)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'icoord': [[25.0, 25.0, 35.0, 35.0],\n",
       "  [55.0, 55.0, 65.0, 65.0],\n",
       "  [45.0, 45.0, 60.0, 60.0],\n",
       "  [115.0, 115.0, 125.0, 125.0],\n",
       "  [105.0, 105.0, 120.0, 120.0],\n",
       "  [95.0, 95.0, 112.5, 112.5],\n",
       "  [135.0, 135.0, 145.0, 145.0],\n",
       "  [175.0, 175.0, 185.0, 185.0],\n",
       "  [165.0, 165.0, 180.0, 180.0],\n",
       "  [155.0, 155.0, 172.5, 172.5],\n",
       "  [140.0, 140.0, 163.75, 163.75],\n",
       "  [205.0, 205.0, 215.0, 215.0],\n",
       "  [195.0, 195.0, 210.0, 210.0],\n",
       "  [151.875, 151.875, 202.5, 202.5],\n",
       "  [103.75, 103.75, 177.1875, 177.1875],\n",
       "  [245.0, 245.0, 255.0, 255.0],\n",
       "  [235.0, 235.0, 250.0, 250.0],\n",
       "  [225.0, 225.0, 242.5, 242.5],\n",
       "  [285.0, 285.0, 295.0, 295.0],\n",
       "  [275.0, 275.0, 290.0, 290.0],\n",
       "  [265.0, 265.0, 282.5, 282.5],\n",
       "  [233.75, 233.75, 273.75, 273.75],\n",
       "  [140.46875, 140.46875, 253.75, 253.75],\n",
       "  [85.0, 85.0, 197.109375, 197.109375],\n",
       "  [75.0, 75.0, 141.0546875, 141.0546875],\n",
       "  [52.5, 52.5, 108.02734375, 108.02734375],\n",
       "  [30.0, 30.0, 80.263671875, 80.263671875],\n",
       "  [15.0, 15.0, 55.1318359375, 55.1318359375],\n",
       "  [5.0, 5.0, 35.06591796875, 35.06591796875]],\n",
       " 'dcoord': [[0.0, 2.165069616081611, 2.165069616081611, 0.0],\n",
       "  [0.0, 1.2583609996700658, 1.2583609996700658, 0.0],\n",
       "  [0.0, 1.8417771551214015, 1.8417771551214015, 1.2583609996700658],\n",
       "  [0.0, 0.5102264872107092, 0.5102264872107092, 0.0],\n",
       "  [0.0, 0.6832038743184952, 0.6832038743184952, 0.5102264872107092],\n",
       "  [0.0, 1.0534720455396414, 1.0534720455396414, 0.6832038743184952],\n",
       "  [0.0, 0.5781192315086323, 0.5781192315086323, 0.0],\n",
       "  [0.0, 0.39279767533924836, 0.39279767533924836, 0.0],\n",
       "  [0.0, 0.6920728341799272, 0.6920728341799272, 0.39279767533924836],\n",
       "  [0.0, 0.8587702247881565, 0.8587702247881565, 0.6920728341799272],\n",
       "  [0.5781192315086323,\n",
       "   0.951351143405573,\n",
       "   0.951351143405573,\n",
       "   0.8587702247881565],\n",
       "  [0.0, 0.5422575852550412, 0.5422575852550412, 0.0],\n",
       "  [0.0, 0.9931260197301512, 0.9931260197301512, 0.5422575852550412],\n",
       "  [0.951351143405573,\n",
       "   1.1337053131831187,\n",
       "   1.1337053131831187,\n",
       "   0.9931260197301512],\n",
       "  [1.0534720455396414,\n",
       "   1.3091848224406866,\n",
       "   1.3091848224406866,\n",
       "   1.1337053131831187],\n",
       "  [0.0, 0.7003307018910611, 0.7003307018910611, 0.0],\n",
       "  [0.0, 0.8455264592324916, 0.8455264592324916, 0.7003307018910611],\n",
       "  [0.0, 1.183614496022609, 1.183614496022609, 0.8455264592324916],\n",
       "  [0.0, 0.7408663414479949, 0.7408663414479949, 0.0],\n",
       "  [0.0, 0.7928025480682006, 0.7928025480682006, 0.7408663414479949],\n",
       "  [0.0, 1.43244903784588, 1.43244903784588, 0.7928025480682006],\n",
       "  [1.183614496022609,\n",
       "   1.4564970761510314,\n",
       "   1.4564970761510314,\n",
       "   1.43244903784588],\n",
       "  [1.3091848224406866,\n",
       "   1.7995913531962198,\n",
       "   1.7995913531962198,\n",
       "   1.4564970761510314],\n",
       "  [0.0, 2.389167635724778, 2.389167635724778, 1.7995913531962198],\n",
       "  [0.0, 2.4533056587669138, 2.4533056587669138, 2.389167635724778],\n",
       "  [1.8417771551214015,\n",
       "   2.801906683718659,\n",
       "   2.801906683718659,\n",
       "   2.4533056587669138],\n",
       "  [2.165069616081611, 4.23103702123038, 4.23103702123038, 2.801906683718659],\n",
       "  [0.0, 5.540472787063805, 5.540472787063805, 4.23103702123038],\n",
       "  [0.0, 8.531300289191663, 8.531300289191663, 5.540472787063805]],\n",
       " 'ivl': ['0',\n",
       "  '24',\n",
       "  '1',\n",
       "  '2',\n",
       "  '29',\n",
       "  '27',\n",
       "  '28',\n",
       "  '3',\n",
       "  '9',\n",
       "  '20',\n",
       "  '16',\n",
       "  '14',\n",
       "  '22',\n",
       "  '13',\n",
       "  '19',\n",
       "  '21',\n",
       "  '26',\n",
       "  '18',\n",
       "  '25',\n",
       "  '17',\n",
       "  '15',\n",
       "  '23',\n",
       "  '5',\n",
       "  '6',\n",
       "  '4',\n",
       "  '7',\n",
       "  '12',\n",
       "  '10',\n",
       "  '8',\n",
       "  '11'],\n",
       " 'leaves': [0,\n",
       "  24,\n",
       "  1,\n",
       "  2,\n",
       "  29,\n",
       "  27,\n",
       "  28,\n",
       "  3,\n",
       "  9,\n",
       "  20,\n",
       "  16,\n",
       "  14,\n",
       "  22,\n",
       "  13,\n",
       "  19,\n",
       "  21,\n",
       "  26,\n",
       "  18,\n",
       "  25,\n",
       "  17,\n",
       "  15,\n",
       "  23,\n",
       "  5,\n",
       "  6,\n",
       "  4,\n",
       "  7,\n",
       "  12,\n",
       "  10,\n",
       "  8,\n",
       "  11],\n",
       " 'color_list': ['g',\n",
       "  'g',\n",
       "  'g',\n",
       "  'g',\n",
       "  'g',\n",
       "  'g',\n",
       "  'g',\n",
       "  'g',\n",
       "  'g',\n",
       "  'g',\n",
       "  'g',\n",
       "  'g',\n",
       "  'g',\n",
       "  'g',\n",
       "  'g',\n",
       "  'g',\n",
       "  'g',\n",
       "  'g',\n",
       "  'g',\n",
       "  'g',\n",
       "  'g',\n",
       "  'g',\n",
       "  'g',\n",
       "  'g',\n",
       "  'g',\n",
       "  'g',\n",
       "  'g',\n",
       "  'g',\n",
       "  'b']}"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAW0AAAD+CAYAAADxhFR7AAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADh0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uMy4xLjEsIGh0dHA6Ly9tYXRwbG90bGliLm9yZy8QZhcZAAAcIElEQVR4nO3deZgdVZnH8e9LAgSCASQtLQSIQAxLgAaaoIDa7ARQRtEZCKBxoV1wwQ1xZVScAR0X3GlEo0KzCMoI7ltUnlGwg40RWVTcUALtM48rMzrqO3+cc0mluu6tup1bt+/p/n2ep5++y7lVb5069dapU3XrmrsjIiJp2Gy6AxARkeqUtEVEEqKkLSKSECVtEZGEKGmLiCRkbh0TXbhwoS9evLiOSYuIzEhr1679rbv3lZWrJWkvXryYsbGxOiYtIjIjmdkvqpTT8IiISEKUtEVEEqKkLSKSECVtEZGEKGmLiCRESV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xJjTHnr8fnjTHnltccx7Kl16BWaQxMHvc+kzAmOOlSsIKyZxCGHraqOO1nEsZ6C8vXXMfZ5bu0KAY2nC9ojE/vT4sxdyafXxgo2uCb1MNbge0qTrdxvmPSdeMFZZcRDlcnDSk1WX/vLWpv7dRbLo6r4/PdY33kL18tmu7F+Q2/xfINEpJ10VU8+bJ7E3rOu1dYvrMIR0ubfD1xL/wV1MUSQq9/0tFtQbvfl8xJ8w7HMUA4Mi+8bLTltKa7UnMLtprMmHMPxNCyB99uvD24fK0OVRvnC+4mnGMoO19Q+fxCm3WcjeMeWn/ZKFv2x8QecbfXdSaOe2qqi7vK1klBvT26ZNqV40jtL7c+7qFg/L1JvZW2+ynG0dhGWrbPptOZ7gqNC1M45tyrMbQbb2rLl/lM6fmCdspPtR7aiaNK2TrX9Uyui5T/Ol1v3Yij6TSmuzJzC7SKkpMavRRDu/GmtHy0f76gcvk267id6W7SOY5pijm5ukjtr856qyuOVn899SMIZmY+zQG1E0O78Sa4fPPc/X/bmHal8lOot8pxtFm2znU9Y+siRXXVW51xNJ3GDF5PIiIzTs/eT1tERCZT0hYRSYiStohIQpS0RUQSoqQtIpIQJW0RkYT8P/ab9ZrMDRteAAAAAElFTkSuQmCC\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "y=pdist(gj)\n",
    "z=linkage(y,'average')\n",
    "dendrogram(z)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "划分成3类的结果如下\n",
      "第1类的有(array([ 1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15, 16, 17,\n",
      "       18, 19, 20, 21, 22, 23, 25, 26, 27, 28, 29], dtype=int64),)\n",
      "第2类的有(array([24], dtype=int64),)\n",
      "第3类的有(array([0], dtype=int64),)\n",
      "划分成4类的结果如下\n",
      "第1类的有(array([1, 2], dtype=int64),)\n",
      "第2类的有(array([ 3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19,\n",
      "       20, 21, 22, 23, 25, 26, 27, 28, 29], dtype=int64),)\n",
      "第3类的有(array([24], dtype=int64),)\n",
      "第4类的有(array([0], dtype=int64),)\n",
      "划分成5类的结果如下\n",
      "第1类的有(array([1, 2], dtype=int64),)\n",
      "第2类的有(array([27, 28, 29], dtype=int64),)\n",
      "第3类的有(array([ 3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19,\n",
      "       20, 21, 22, 23, 25, 26], dtype=int64),)\n",
      "第4类的有(array([24], dtype=int64),)\n",
      "第5类的有(array([0], dtype=int64),)\n"
     ]
    }
   ],
   "source": [
    "for k in range(3,6):\n",
    "    print('划分成{}类的结果如下'.format(k))\n",
    "    T=fcluster(z,k,'maxclust')\n",
    "    for i in range(1,k+1):\n",
    "        tm=np.where(T==i)\n",
    "        print('第{}类的有{}'.format(i,tm))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
